/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sisgraf.dao;
import sisgraf.model.ItemOrcamento;
import sisgraf.util.HibernateUtil;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import sisgraf.model.FichaCliente;
import sisgraf.model.Pedido;
/**
 *
 * @author Jordan de Carvalho Guerra
 */
public class ItemOrcamentoDaoImp  implements ItemOrcamentoDao {
    private Session sessionLock;
    private Transaction transacaoLock;
    public boolean save(ItemOrcamento itemOrcamento) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.save(itemOrcamento);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
    public boolean update(ItemOrcamento itemOrcamento) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.update(itemOrcamento);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
    public ItemOrcamento getItemOrcamento(int id) {
        ItemOrcamento itemOrcamento=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            itemOrcamento= (ItemOrcamento) session.get(ItemOrcamento.class, id);
        } catch(Exception e)
        {
            itemOrcamento=null;
            if (session != null){
                session.close();
            }
        } finally
        {
            return itemOrcamento;
        }
    }
    public ItemOrcamento getLockItemOrcamento(int id) {
        ItemOrcamento itemOrcamento=null;
        try
        {
            this.sessionLock = HibernateUtil.getSessionFactory().openSession();
            this.transacaoLock = this.sessionLock.beginTransaction();
            itemOrcamento = (ItemOrcamento) this.sessionLock.get(ItemOrcamento.class, id, LockMode.UPGRADE);
        } catch(Exception e) {
            itemOrcamento=null;
            if (this.transacaoLock != null){
                this.transacaoLock.rollback();
            }
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return itemOrcamento;
        }
    }
    public boolean settLockItemOrcamento(ItemOrcamento itemOrcamento) {
        boolean retorno=true;
//        int id = itemOrcamento.getId();
        try
        {
            this.sessionLock.update(itemOrcamento);
            this.transacaoLock.commit();
        } catch(Exception e) {
            retorno=false;
            if (this.transacaoLock != null){
                this.transacaoLock.rollback();
            }
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return retorno;
        }
    }
    public boolean unLockItemOrcamento() {
        boolean retorno=true;
        try
        {
            this.transacaoLock.rollback();
        } catch(Exception e) {
            retorno=false;
            if (this.sessionLock != null){
                this.sessionLock.close();
            }
        } finally
        {
            return retorno;
        }
    }
    public List<ItemOrcamento> list(String par) {
        List lista=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            String sql = "from ItemOrcamento";
            if (!par.equals("")) sql=sql + " i where "+par;
            lista = session.createQuery(sql).list();
        } catch(Exception e)
        {
            lista=null;
            if (session != null){
                session.close();
            }
        } finally
        {
            return lista;
        }
    }
    public List<FichaCliente> listFicha(String par) {
        List lista=null;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            String sql = "select new sisgraf.model.FichaCliente(i, p) "+
                            " from sisgraf.model.Pedido p "+
                            " right join p.itemOrcamento as i ";
            if (!par.equals("")) sql=sql + " where "+par;
            lista = session.createQuery(sql).list();
        } catch(Exception e)
        {
            lista=null;
            if (session != null){
                session.close();
            }
        } finally
        {
            return lista;
        }
    }
    public boolean remove(ItemOrcamento itemOrcamento) {
        boolean retorno = true;
        Session session = null;
        Transaction t=null;
        try
        {
            session = HibernateUtil.getSessionFactory().openSession();
            t = session.beginTransaction();
            session.delete(itemOrcamento);
            t.commit();
        } catch(Exception e)
        {
            retorno=false;
            if (t != null){
                t.rollback();
            }
        } finally
        {
            if (session != null){
                session.close();
            }
            return retorno;
        }
    }
}
